<template>
  <el-dialog
    :width="'50rem'"
    :visible="dialogVisible"
    title="异常详情"
    @close="$emit('close')"
  >
    <el-pagination
      style="float: right;"
      layout="prev, pager, next,sizes, total"
      :total="total"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      :current-page="page+1"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-table
      v-loading="loading"
      :data="data"
    >
      <el-table-column
        label="序号"
        type="index"
        width="55px"
      />
      <el-table-column
        label="集群名称"
        prop="cmName"
        show-overflow-tooltip
      />
      <el-table-column
        label="子集群名称"
        prop="clusterName"
        show-overflow-tooltip
      />
      <el-table-column
        label="队列名称"
        prop="queue"
        show-overflow-tooltip
      />
      <el-table-column
        label="异常次数"
        prop="countA"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="!isResPool"
        label="异常时间点"
        prop="createTime"
        show-overflow-tooltip
      />
    </el-table>
  </el-dialog>
</template>

<script>
import resource from '@/api/monitor/resource'
import resourceApi from '@/api/monitor/resource'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    cmFlag: {
      type: String,
      require: true
    },
    clusterFlag: {
      type: String,
      require: true
    },
    isResPool: {
      type: Boolean,
      require: true
    },
    resource: {
      type: String,
      require: true
    }
  },
  emits: ['close'],
  data() {
    return {
      page: 0,
      size: 10,
      data: [],
      total: 0,
      loading: false
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.page = 0
        this.size = 10
        this.getData()
      }
    }
  },
  methods: {
    async getData(resetPage = true) {
      this.loading = true
      this.page = resetPage ? 0 : this.page
      const params = {
        cmFlag: this.cmFlag,
        clusterFlag: this.clusterFlag,
        page: this.page,
        size: this.size
      }
      let apiMethod

      if (this.isResPool) {
        apiMethod = this.resource === 'yarn' ? resourceApi.selYarnPoolAll : resourceApi.selImpalaPoolAll
      } else {
        apiMethod = this.resource === 'yarn' ? resourceApi.selYarnPoolYc : resourceApi.selImpalaPoolYc
      }
      const res = await apiMethod(params)
      this.data = res.content
      this.total = res.totalElements
      this.loading = false
    },
    handleCurrentChange(page) {
      this.page = page - 1
      this.getData(false)
    },
    handleSizeChange(size) {
      this.size = size
      this.getData(true)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
